<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['putSorage:pmmMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          :disabled="type1"
          v-hasPermi="['putSorage:pmmMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="type1"
          @click="handleDelete"
          v-hasPermi="['putSorage:pmmMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="type1"
          @click="handleSubmit"
          v-hasPermi="['putSorage:pmmMstr:submit']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDc('#exportDj', '生产退料单据')"
          v-hasPermi="['putSorage:pmmMstr:export']"
        >
          EXCEL
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDc('#exportMx', '生产退料明细')"
          v-hasPermi="['putSorage:pmmMstr:export']"
        >
          退料明细
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDc('#exportTj', '生产退料统计')"
          v-hasPermi="['putSorage:pmmMstr:export']"
        >
          退料统计
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="pickingList"
      :height="tableH"
      @row-click="clickRow"
      id="exportDj"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        label="#"
        type="index"
        fixed="left"
        width="40"
        align="center"
      />
      <el-table-column
        label="退料单号"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="pmmDjbh"
      />
      <el-table-column
        label="退料日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pmmLlrq"
        width="180"
      />
      <!--      <el-table-column label="单据类型" :show-overflow-tooltip="true" width="100" align="center" prop="pmmType">
        <template #default="scope">
          <dict-tag :options="pmmTypeOptions" :value="scope.row.pmmType" />
        </template>
      </el-table-column>-->
      <el-table-column
        label="仓库代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.pmmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="退料部门"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmBmdm"
      />
      <el-table-column
        label="单据描述"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmDesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmJlry"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmNgdesc"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pmmJlrq"
        width="180"
      />
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pmmShrq"
        width="180"
      />
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pmmGzrq"
        width="180"
      />
      <el-table-column
        label="原因代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmYydm"
      >
        <template #default="scope">
          <dict-tag :options="pmmYydmOptions" :value="scope.row.pmmYydm" />
        </template>
      </el-table-column>
      <el-table-column
        label="原因描述"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pmmYyms"
      />
      <el-table-column
        label="操作"
        align="center"
        width="130"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.pmmTjflag === '1'"
            v-show="scope.row.pmmTjflag !== '1'"
            @click="handleAdded(scope.row)"
            v-hasPermi="['putSorage:pmmMstr:addpmd']"
          >
            添加
          </el-button>
          <el-button size="mini" type="text" @click="handleDjDy(scope.row)"
            >单据打印</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--明细-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="退料明细" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          :data="chdDetList"
          id="exportMx"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="制造单号"
            align="center"
            prop="pmdZzdh"
            fixed="left"
          />
          <el-table-column label="退料物料代码" align="center" prop="pmdWldm" />
          <el-table-column label="申请数量" align="center" prop="pmdSqsl" />
          <el-table-column label="入库数量" align="center" prop="pmdCksl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            :show-overflow-tooltip="true"
            align="center"
            prop="pmdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.pmdCwdm" />
            </template>
          </el-table-column>
          <!--          <el-table-column label="出库人员" align="center" prop="pmdCkry" />
          <el-table-column label="出库日期" align="center" prop="pmdCkrq" />-->
          <el-table-column label="规格描述" align="center" prop="itmPmgg" />
          <el-table-column
            label="客户物料编码"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column label="最小包装" align="center" prop="pmdMinbz" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                :disabled="type1"
                @click="deleteChdDet(scope.row)"
                v-hasPermi="['output:picking:removepmd']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="退料统计" name="second">
        <el-table
          border
          stripe
          id="exportTj"
          :height="tableHChild"
          :data="chdDetTotalList"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="客户代码"
            align="center"
            prop="itmCustwldm"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="pmdWldm" />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="存放位置" align="center" prop="pmdCwdm">
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.pmdCwdm" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="pmdMinbz" />
          <el-table-column label="申请数量" align="center" prop="pmdSqsl" />
          <el-table-column label="入库数量" align="center" prop="pmdCksl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!--    添加对话框-->
    <el-dialog
      v-model="openFull"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
      class="dia-container"
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="制造单号" prop="pmmZzdh">
              <el-input
                v-model="queryChildParams.pmmZzdh"
                placeholder="请输入制造单号"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="成品综合查询" prop="pmmZzdh">
              <el-input
                v-model="queryChildParams.pmmCpDesc"
                placeholder="请输入成品信息"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="综合查询" prop="integratedQuery">
              <el-input
                v-model="queryChildParams.integratedQuery"
                placeholder="综合查询"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车间" prop="pmmCj">
              <el-radio-group v-model="queryChildParams.pmmCj" size="medium">
                <el-radio
                  v-for="dict in itCarOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  :value="dict.dictValue"
                >
                  {{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="serachChildrened"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="12" style="text-align: right">
            <Tag type="dot" size="default" color="#d39207">代用料</Tag>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <!--      @row-click="clickdel"-->
      <el-table
        highlight-current-row
        v-loading="loading"
        :data="serachAddList"
        border
        stripe
        :height="tableHAdd"
        @row-click="tableAddClick"
        :row-class-name="({ row }) => (row.dyFlag === 1 ? 'update' : '')"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column
          label="制造单号"
          width="145"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdZzdh"
        />
        <el-table-column
          label="计划工单数量"
          width="100"
          :show-overflow-tooltip="true"
          align="center"
          prop="modDdsl"
        />
        <el-table-column
          label="退料物料代码"
          width="100"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdWldm"
        />
        <el-table-column
          label="父项物料代码"
          width="100"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdFxdm"
        />
        <el-table-column
          label="采购属性"
          width="80"
          :show-overflow-tooltip="true"
          align="center"
          prop="modCgsx"
        >
          <template #default="scope">
            <dict-tag :options="bomCgsxOptions" :value="scope.row.modCgsx" />
          </template>
        </el-table-column>
        <el-table-column
          label="单位"
          width="80"
          :show-overflow-tooltip="true"
          align="center"
          prop="itmDw"
        >
          <template #default="scope">
            <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
          </template>
        </el-table-column>
        <el-table-column
          label="储位信息"
          min-width="120"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdCwdm"
        >
          <template #default="scope">
            <dict-tag :options="allCwOptions" :value="scope.row.pmdCwdm" />
          </template>
        </el-table-column>
        <el-table-column
          label="最小包装"
          width="80"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdMinbz"
        />
        <el-table-column
          label="销售单号"
          width="120"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdSodh"
        ></el-table-column>
        <el-table-column
          label="退料品名规格"
          min-width="250"
          fixed="right"
          :show-overflow-tooltip="true"
          prop="itmPmgg"
        />
        <el-table-column
          label="父项客户编码"
          min-width="100"
          fixed="right"
          :show-overflow-tooltip="true"
          prop="itmFxCustwldm"
        />
        <el-table-column
          label="父项品名规格"
          min-width="250"
          fixed="right"
          :show-overflow-tooltip="true"
          prop="itmFxPmgg"
        />
        <el-table-column
          label="工单需求数"
          width="90"
          fixed="right"
          :show-overflow-tooltip="true"
          align="center"
          prop="modXqsl"
        />
        <el-table-column
          label="已退料数"
          width="90"
          fixed="right"
          :show-overflow-tooltip="true"
          align="center"
          prop="modYtsl"
        />
        <el-table-column
          label="待退料数量"
          width="100"
          fixed="right"
          :show-overflow-tooltip="true"
          align="center"
          prop="modDsqsl"
        />
        <el-table-column
          label="需退料数"
          width="80"
          fixed="right"
          align="center"
          prop="pmdSqsl"
        >
          <template #default="scope">
            <el-input
              class="inputName"
              @input="
                (e) => {
                  changValue(scope.row, e);
                }
              "
              type="number"
              v-model="scope.row.pmdSqsl"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="queryChildParams.pageSize"
        :current-page.sync="queryChildParams.pageNum"
        @current-change="load2"
        layout="total, prev, pager, next,jumper"
        :total="total2"
        style="text-align: right"
      ></el-pagination>
      <!--明细-->
      <el-tabs v-model="activeTab">
        <el-tab-pane label="退料明细" name="first">
          <el-table
            border
            stripe
            :height="tableHAddChild"
            :data="chdDetList"
            :header-cell-style="{ 'text-align': 'center' }"
          >
            <el-table-column
              label="#"
              type="index"
              align="center"
              fixed="left"
            />
            <el-table-column
              label="制造单号"
              align="center"
              prop="pmdZzdh"
              fixed="left"
            />
            <el-table-column
              label="退料物料代码"
              align="center"
              prop="pmdWldm"
            />
            <el-table-column label="申请数量" align="center" prop="pmdSqsl" />
            <el-table-column label="入库数量" align="center" prop="pmdCksl" />
            <el-table-column
              label="单位"
              :show-overflow-tooltip="true"
              align="center"
              prop="itmDw"
            >
              <template #default="scope">
                <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
              </template>
            </el-table-column>
            <el-table-column
              label="存放位置"
              :show-overflow-tooltip="true"
              align="center"
              prop="pmdCwdm"
            >
              <template #default="scope">
                <dict-tag :options="allCwOptions" :value="scope.row.pmdCwdm" />
              </template>
            </el-table-column>
            <!--            <el-table-column label="出库人员" align="center" prop="pmdCkry" />
            <el-table-column label="出库日期" align="center" prop="pmdCkrq" />-->
            <el-table-column label="规格描述" align="center" prop="itmPmgg" />
            <el-table-column
              label="客户物料编码"
              align="center"
              prop="itmCustwldm"
            />
            <el-table-column label="最小包装" align="center" prop="pmdMinbz" />
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template #default="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="deleteChdDet(scope.row)"
                  v-hasPermi="['output:picking:removepmd']"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="退料统计" name="second">
          <el-table
            border
            stripe
            :height="tableHAddChild"
            :data="chdDetTotalList"
          >
            <el-table-column
              label="#"
              type="index"
              align="center"
              fixed="left"
            />
            <el-table-column
              label="客户代码"
              align="center"
              prop="itmCustwldm"
              fixed="left"
            />
            <el-table-column label="物料代码" align="center" prop="pmdWldm" />
            <el-table-column
              label="品名规格"
              align="center"
              :show-overflow-tooltip="true"
              prop="itmPmgg"
            />
            <el-table-column
              label="单位"
              :show-overflow-tooltip="true"
              align="center"
              prop="itmDw"
            >
              <template #default="scope">
                <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
              </template>
            </el-table-column>
            <el-table-column label="存放位置" align="center" prop="itmCwdm" />
            <el-table-column label="最小包装" align="center" prop="pmdMinbz" />
            <el-table-column label="申请数量" align="center" prop="pmdSqsl" />
            <el-table-column label="入库数量" align="center" prop="pmdCksl" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <div style="margin-top: 25px; display: flex; justify-content: flex-end">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handAddChildren">确认</el-button>
      </div>
    </el-dialog>
    <!--    添加对话框-->
    <el-dialog
      v-model="openFullFei"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="20">
            <el-form-item label="综合查询" prop="integratedQuery">
              <el-input
                v-model="queryChildParams.integratedQuery"
                placeholder="综合查询"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="serachChildrened"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <!--      @row-click="clickdel"-->
      <el-table
        highlight-current-row
        v-loading="loading"
        :data="serachAddList"
        border
        stripe
        :height="tableHAdd"
        @scrollBottom="load2"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column label="#" min-width="50" type="index" align="center" />
        <el-table-column type="selection" min-width="50" align="center" />
        <el-table-column
          label="物料代码"
          min-width="135"
          align="center"
          :show-overflow-tooltip="true"
          prop="pmdWldm"
        />
        <el-table-column
          label="品名规格"
          min-width="400"
          :show-overflow-tooltip="true"
          prop="itmPmgg"
        />
        <el-table-column
          label="单位"
          min-width="80"
          :show-overflow-tooltip="true"
          align="center"
          prop="itmDw"
        >
          <template #default="scope">
            <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
          </template>
        </el-table-column>
        <el-table-column
          label="储位信息"
          min-width="120"
          :show-overflow-tooltip="true"
          prop="pmdCwdm"
        >
          <template #default="scope">
            <dict-tag :options="allCwOptions" :value="scope.row.pmdCwdm" />
          </template>
        </el-table-column>
        <el-table-column
          label="最小包装"
          min-width="100"
          :show-overflow-tooltip="true"
          align="center"
          prop="pmdMinbz"
        />
        <el-table-column
          label="已申请数"
          min-width="100"
          :show-overflow-tooltip="true"
          align="center"
          prop="modYtsl"
        />
        <el-table-column
          label="需申请数"
          min-width="100"
          align="center"
          prop="pmdSqsl"
        >
          <template #default="scope">
            <el-input
              class="inputName"
              @input="
                (e) => {
                  changValue(scope.row, e);
                }
              "
              type="number"
              v-model="scope.row.pmdSqsl"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="queryChildParams.pageSize"
        :current-page.sync="queryChildParams.pageNum"
        @current-change="load2"
        layout="total, prev, pager, next"
        :total="total2"
        style="text-align: right"
      ></el-pagination>
      <div style="margin-top: 25px; display: flex; justify-content: flex-end">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handAddChildren">确认</el-button>
      </div>
    </el-dialog>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="退料日期">
            <el-date-picker
              v-model="daterangepmmLlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退料单号" prop="pmmDjbh">
            <el-input
              v-model="queryParams.pmmDjbh"
              placeholder="请输入退料单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="制造单号" prop="pmdZzdh">
            <el-input
              v-model="queryParams.pmdZzdh"
              placeholder="请输入制造单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="24">
          <el-form-item label="品名规格" prop="pmmPmgg">
            <el-input
              v-model="queryParams.pmmPmgg"
              placeholder="请输入品名规格"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>-->
        <el-col :span="24">
          <el-form-item label="仓库" prop="pmmCkdm">
            <el-select
              size="small"
              style="width: 240px"
              v-model="queryParams.pmmCkdm"
              placeholder="请选择仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-form-item>
            <el-radio-group v-model="queryParams.pmmZtbz">
              <el-radio
                v-for="dict in cjmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 添加或修改计划退料开单作业对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="退料单号" prop="pmmDjbh">
            <el-input style="width: 240px" v-model="form.pmmDjbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开单日期" prop="pmmLlrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.pmmLlrq"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择退料日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">
          <el-form-item label="退料类型" prop="pmmType">
            <el-select v-model="form.pmmType" style="width: 240px" placeholder="请选择单据类型">
              <el-option v-for="dict in pmmTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>-->
        <el-col :span="24">
          <el-form-item label="仓库代码" prop="pmmCkdm">
            <el-select
              v-model="form.pmmCkdm"
              placeholder="请选择仓库代码"
              :disabled="
                chdDetList.length > 0 && this.title.indexOf('修改') > -1
              "
            >
              <el-option
                v-for="dict in storageOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单据描述" prop="pmmDesc">
            <el-input
              style="width: 240px"
              v-model="form.pmmDesc"
              placeholder="请输入单据描述"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原因代码" prop="pmmYydm">
            <el-select
              v-model="form.pmmYydm"
              placeholder="请选择原因代码"
              @change="changeDescriptionFlag"
              clearable
            >
              <el-option
                v-for="dict in pmmYydmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="原因描述" prop="pmmYyms">
            <el-input
              v-model="form.pmmYyms"
              type="textarea"
              placeholder="请输入内容"
              :disabled="reasonDescriptionFlag"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCgList } from "@/api/project/mstr";
import XLSX from "xlsx";
import { exportTable, printOrder } from "@/utils/ruoyi";
import {
  addPmmMstr,
  addPmmMstrChild,
  delPmmMstr,
  delPmmMstrChild,
  getPmmMstr,
  listPmmMstr,
  selectPmmMstrAddChild,
  selectPmmMstrDetailAndTotalByOrder,
  submitPmmMstr,
  updatePmmMstr,
} from "@/api/putStorage/pmmMstr";

export default {
  name: "Pmm",
  data() {
    return {
      // 遮罩层
      loading: true,
      openFull: false,
      openFullFei: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      // 计划退料开单作业表格数据
      pickingList: [],
      chdDetList: [],
      chdDetTotalList: [],
      pmdZzdh: "",
      serachAddList: [],
      tableH: 250,
      tableHChild: 300,
      tableHAdd: 230,
      tableHAddChild: 200,
      activeTab: "first",
      //所有仓库
      allStorageOptions: [],
      //有效仓库
      storageOptions: [],
      rowdata: [],
      //当前数据
      rowData: null,
      typeChild: null,
      //选择标识
      type1: true,
      //原因描述flag
      reasonDescriptionFlag: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 单据类型字典
      pmmTypeOptions: [],
      // 原因代码字典
      pmmYydmOptions: [],
      itCarOptions: [],
      // 退料日期时间范围
      daterangepmmLlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      unitFlagOptions: [],
      bomCgsxOptions: [],
      allCwOptions: [],
      cjmZtbzOptions: [],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        pmmDjbh: null,
        pmdZzdh: null,
        pmmLlrq: null,
        pmmCkdm: null,
        pmmPmgg: null,
        pmmZtbz: "1",
      },
      // 添加物料-查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 8,
        pmmZzdh: null,
        pmmCpDesc: null,
        pmmCj: "mp_000",
        integratedQuery: null,
        pmmDjbh: null,
        pmmCkdm: null,
        pmmType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // pmmType: [{ required: true, message: '单据类型不能为空', trigger: 'change' }],
        pmmCkdm: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    //制造车间字典
    //单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    this.getDicts("sys_cjm_ztbz").then((response) => {
      this.cjmZtbzOptions = response.data;
    });
    //得到采购属性
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
    this.getDicts("Manufacturing_plant").then((response) => {
      this.itCarOptions = response.data;
      if (this.$store.state.settings.sysName.indexOf("荆州") > -1) {
        this.itCarOptions = this.itCarOptions.filter((o) => {
          return o.dictLabel !== "所有";
        });
        this.queryChildParams.pmmCj = "mp_004";
      }
    });
    this.getDicts("production_pick_type").then((response) => {
      this.pmmTypeOptions = response.data;
    });
    this.getDicts("reason_category").then((response) => {
      this.pmmYydmOptions = response.data;
    });
    //所有仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //所有储位
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;
        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
  },
  methods: {
    /** 查询计划退料开单作业列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangepmmLlrq && "" != this.daterangepmmLlrq) {
        this.queryParams.params["beginpmmLlrq"] = this.moment(
          this.daterangepmmLlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endpmmLlrq"] = this.moment(
          this.daterangepmmLlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listPmmMstr(this.queryParams).then((response) => {
        this.pickingList = [];
        this.pickingList = this.pickingList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.pickingList != null && this.pickingList.length > 0) {
          this.clickRow(this.pickingList[0]);
        } else {
          this.chdDetTotalList = [];
          this.chdDetList = [];
        }
      });
    },
    handleAdded(row) {
      this.typeChild = row.pmmType;
      if (row.pmmType === "C") {
        this.openFullFei = true;
      } else {
        this.openFull = true;
      }
      this.queryChildParams.pmmDjbh = row.pmmDjbh;
      this.queryChildParams.pmmCkdm = row.pmmCkdm;
      this.queryChildParams.pmmType = row.pmmType;

      this.serachChildrened();
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listPmmMstr(this.queryParams).then((response) => {
          this.pickingList = this.pickingList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    load2() {
      //if (this.queryChildParams.pageNum < Math.ceil(this.total2 / this.queryChildParams.pageSize)) {
      // this.queryChildParams.pageNum++
      selectPmmMstrAddChild(this.queryChildParams).then((response) => {
        this.serachAddList = response.rows;
        this.total2 = response.total;
        this.loading = false;
      });
      //}
      return 0;
    },
    serachChildrened() {
      this.queryChildParams.pageNum = 1;
      selectPmmMstrAddChild(this.queryChildParams).then((res) => {
        this.serachAddList = [];
        this.serachAddList = this.serachAddList.concat(res.rows);
        this.total2 = res.total;
      });
    },
    handAddChildren() {
      const tmpData = this.serachAddList.filter((a) => {
        return !!a.pmdSqsl && a.pmdSqsl > 0;
      });
      if (!tmpData.length > 0) {
        this.msgError("请输入需要添加的物料");
        return;
      }
      addPmmMstrChild(tmpData)
        .then((response) => {
          this.msgSuccess("添加成功");
          this.serachAddList = [];
          //this.serachChildrened()
          this.load2();
          this.getChdDetList(this.queryChildParams.pmmDjbh);
        })
        .catch(() => {
          this.msgError("添加失败");
        });
    },
    /** 删除子项明细操作 */
    deleteChdDet(row) {
      delPmmMstrChild(row.pmdId)
        .then((res) => {
          this.msgSuccess("删除成功~");
          this.getChdDetList(row.pmdDjbh);
          this.serachChildrened();
        })
        .catch(() => {
          this.msgError("删除失败~");
        });
    },

    getChdDetList(chdDjbh) {
      selectPmmMstrDetailAndTotalByOrder(chdDjbh).then((response) => {
        this.chdDetTotalList = response.data.list2;
        this.chdDetList = response.data.list1;
      });
    },
    tableAddClick(row) {
      this.pmdZzdh = row.pmdZzdh;
      // getProductMod(row.pmdZzdh).then((response) => {
      //   this.listProductBom = response.data
      // })
    },
    changValue(row, e) {
      let pmdSqsl = parseFloat(e);
      row.pmdSqsl = Math.min(pmdSqsl, row.modDsqsl);
      /*if (this.typeChild == 'A') {
        const max = Math.min(row.modDsqsl, row.kcmKykc)
        if (pmdSqsl > max) {
          this.msgError('可用库存不足~')
          row.pmdSqsl = max
          e = row.pmdSqsl
          return
        }
      } else if (this.typeChild == 'B') {
        if (pmdSqsl > row.kcmKykc) {
          this.msgError('可用库存不足~')
          row.pmdSqsl = row.kcmKykc
          e = row.pmdSqsl
          return
        }
      }
      //非计划
      if (this.openFullFei) {
        if (pmdSqsl > row.kcmKykc) {
          this.msgError('可用库存不足~')
          row.pmdSqsl = row.kcmKykc
          e = row.pmdSqsl
          return
        }
      }*/
      // 同一物料申请的总数据不能超过可用库存
      /* const list = this.serachAddList.filter((a) => a.pmdWldm === row.pmdWldm)
      const count = list.reduce((p, c) => p + parseInt(c.pmdSqsl || 0), 0)
      if (row.kcmKykc < count) {
        this.msgError('同一物料申请的总数据不能超过可用库存.')
        let numKy = parseInt(row.kcmKykc - (count - e))
        row.pmdSqsl = numKy
        e = row.pmdSqsl
        return
      }*/
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.pmmZtbz == "1" && row.pmmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.pmmZtbz == "2" && row.pmmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.pmmZtbz == "6" && row.pmmTjflag == "1") {
        return "color:#bb0331";
      }
    },
    clickRow(row) {
      // //获得选中的详细数据
      this.rowData = row;
      // //获得当前单据的状态
      // this.ids = row.rwmId
      this.type1 = !(row != null && row.pmmZtbz == "1" && row.pmmTjflag == "0");
      //获得选中的详细数据
      this.getChdDetList(row.pmmDjbh);
    },
    // getpmdDetList(data) {
    //
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openFull = false;
      this.openFullFei = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pmmId: null,
        pmmDjbh: null,
        pmmType: "A",
        pmmCkdm: null,
        pmmBmdm: null,
        pmmDesc: null,
        pmmTjflag: null,
        pmmNgdesc: null,
        pmmZtbz: null,
        pmmJlry: null,
        pmmJlrq: null,
        pmmShry: null,
        pmmShrq: null,
        pmmGzry: null,
        pmmGzrq: null,
        pmmYydm: null,
        pmmYyms: null,
        pmmLlrq: this.moment().format("YYYY-MM-DD"),
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pickingList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangepmmLlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.pmmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计划退料开单作业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pmmId = row.pmmId || this.rowData.pmmId;
      getPmmMstr(pmmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计划退料开单作业";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询计划退料开单作业";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.pmmBmdm = this.$store.state.user.deptName;
          this.form.pmmLlrq = this.moment(this.form.pmmLlrq).format();
          if (this.form.pmmId != null) {
            updatePmmMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPmmMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDc(a, b) {
      exportTable(a, b);
    },
    /** 提交按钮 */
    handleSubmit() {
      if (this.rowData.pmmDjbh == null) {
        this.msgError("请选择需要提交的单据");
      } else {
        selectPmmMstrDetailAndTotalByOrder(this.rowData.pmmDjbh).then((res) => {
          if (res.data.list1.length <= 0) {
            this.msgError("该单据下没有物料,无法提交");
          } else {
            submitPmmMstr(this.rowData.pmmId)
              .then((res) => {
                this.msgSuccess("提交成功");
                this.getList();
              })
              .catch(() => {
                this.msgError("提交失败");
              });
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pmmIds = row.pmmId || this.rowData.pmmId;
      this.$confirm(
        '是否确认删除计划退料开单作业编号为"' + pmmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delPmmMstr(pmmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //改变原因描述状态
    changeDescriptionFlag(data) {
      if (data) {
        this.reasonDescriptionFlag = false;
      } else {
        this.reasonDescriptionFlag = true;
      }
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams
    //   this.$confirm('是否确认导出所有计划退料开单作业数据项?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     this.exportLoading = true
    //     return exportPicking(queryParams)
    //   }).then(response => {
    //     this.download(response.msg)
    //     this.exportLoading = false
    //   }).catch(() => {
    //   })
    // },
    handleExport() {
      console.log("进入到这个方法");
      //根据给的id获取table表，选取元素的时候加上，{raw:true}可以使表格正常导出，消除科学计数法
      let wb = XLSX.utils.table_to_book(document.getElementById("exportXlsx"), {
        raw: true,
      });

      let wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        //给xlsx文件赋值名字
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          "test.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      console.log("走出到这个方法1");
      return wbout;
    },
    handleDjDy(row) {
      const data = { pmmDjbh: row.pmmDjbh };
      const pmmDjbh = row.pmmDjbh;
      if (row) {
        if (row.pmmTjflag !== "1") {
          this.$confirm("当前单据还未提交，是否过账并执行打印？", "温馨提醒", {
            confirmButtonText: "提交并打印",
            cancelButtonText: "取消",
            type: "info",
          }).then((res) => {
            if (pmmDjbh) {
              selectPmmMstrDetailAndTotalByOrder(pmmDjbh).then((res) => {
                if (res.data.list1.length <= 0) {
                  this.msgError("该单据下没有物料,无法提交");
                } else {
                  submitPmmMstr(this.rowData.pmmId)
                    .then((res) => {
                      this.msgSuccess("提交成功");
                      this.getList();
                      printOrder(
                        "/putStorage/pmmMstr/exportMaterialReturnTemplate",
                        data,
                        "计划退料单据"
                      );
                    })
                    .catch(() => {
                      this.msgError("提交失败");
                    });
                }
              });
            } else {
              this.msgError("无提交单据");
            }
          });
          return;
        }
      }

      printOrder(
        "/putStorage/pmmMstr/exportMaterialReturnTemplate",
        data,
        "计划退料单据"
      );
    },
  },
};
</script>

<style scoped lang="scss"></style>
